// StartCalcParam.cpp : implementation file
//

#include "stdafx.h"
#include "g_prog1.h"
#include "StartCalcParamDlg.h"
#include "EphemWrapper.h"


// CStartCalcParam dialog

IMPLEMENT_DYNAMIC(CStartCalcParamDlg, CDialog)

CStartCalcParamDlg::CStartCalcParamDlg(CWnd* pParent /*=NULL*/)
: CDialog(CStartCalcParamDlg::IDD, pParent)
, m_dShift(0.0)
, m_dFromRange(0.0)
, m_dToRange(0.0)
, m_nCount(0)
, m_useKM(FALSE)
, m_bRangeSame(FALSE)
, m_dFromTime(0.0)
, m_dToTime(0.0)
, m_dFromDefTime(0.0)
, m_dToDefTime(0.0)
, m_dFromSelTime(0.0)
, m_dToSelTime(0.0)
, m_bUseSelectTime(FALSE)
{
	m_nCount = m_prm.m_nStepCount;
	m_useKM = m_prm.m_nUseKm;
	m_bRangeSame = m_prm.m_nSameRange;
	m_bUseSelectTime = m_prm.m_nUseSelectedTime;
}

CStartCalcParamDlg::~CStartCalcParamDlg()
{
}

void CStartCalcParamDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_SHIFT, m_dShift);
	DDX_Text(pDX, IDC_EDIT_FROM, m_dFromRange);
	DDX_Text(pDX, IDC_EDIT_TO, m_dToRange);
	DDX_Text(pDX, IDC_EDIT_STEPS, m_nCount);
	DDV_MinMaxInt(pDX, m_nCount, 0, 1000000);
	DDX_Check(pDX, IDC_CHECK_USE_KM, m_useKM);
	DDX_Check(pDX, IDC_CHECK_SAME, m_bRangeSame);
	DDX_Text(pDX, IDC_EDIT_TIME_FROM, m_dFromTime);
	DDX_Text(pDX, IDC_EDIT_TIME_TO, m_dToTime);
	DDX_Check(pDX, IDC_CHECK_USE_SELECT_TIME, m_bUseSelectTime);
}


BEGIN_MESSAGE_MAP(CStartCalcParamDlg, CDialog)
	ON_BN_CLICKED(IDC_CHECK_USE_KM, &CStartCalcParamDlg::OnBnClickedCheckUseKm)
	ON_EN_CHANGE(IDC_EDIT_FROM, &CStartCalcParamDlg::OnEnChangeEditFrom)
	ON_EN_CHANGE(IDC_EDIT_TO, &CStartCalcParamDlg::OnEnChangeEditTo)
	ON_BN_CLICKED(IDC_BUTTON_FROM_SEL, &CStartCalcParamDlg::OnBnClickedButtonFromSel)
	ON_BN_CLICKED(IDC_BUTTON_FROM_DEF, &CStartCalcParamDlg::OnBnClickedButtonFromDef)
	ON_BN_CLICKED(IDC_BUTTON_TO_SEL, &CStartCalcParamDlg::OnBnClickedButtonToSel)
	ON_BN_CLICKED(IDC_BUTTON_TO_DEF, &CStartCalcParamDlg::OnBnClickedButtonToDef)
	ON_EN_CHANGE(IDC_EDIT_TIME_FROM, &CStartCalcParamDlg::OnEnChangeEditTimeFrom)
	ON_EN_CHANGE(IDC_EDIT_TIME_TO, &CStartCalcParamDlg::OnEnChangeEditTimeTo)
	ON_BN_CLICKED(IDC_CHECK_USE_SELECT_TIME, &CStartCalcParamDlg::OnBnClickedCheckUseSelectTime)
END_MESSAGE_MAP()


void CStartCalcParamDlg::OnBnClickedCheckUseKm()
{
	UpdateData(TRUE);
	if(m_useKM)
	{
		m_dShift = CEphemWrapper::AUtoKM(m_dShift);
		m_dFromRange = CEphemWrapper::AUtoKM(m_dFromRange);
		m_dToRange = CEphemWrapper::AUtoKM(m_dToRange); 
	}
	else
	{
		m_dShift = CEphemWrapper::KMtoAU(m_dShift);
		m_dFromRange = CEphemWrapper::KMtoAU(m_dFromRange);
		m_dToRange = CEphemWrapper::KMtoAU(m_dToRange); 
	}
	UpdateData(FALSE);
}

void CStartCalcParamDlg::OnOK()
{
	UpdateData(TRUE);
	if(m_useKM)
	{
		m_dShift = CEphemWrapper::KMtoAU(m_dShift);
		m_dFromRange = CEphemWrapper::KMtoAU(m_dFromRange);
		m_dToRange = CEphemWrapper::KMtoAU(m_dToRange); 
	}

	m_prm.m_nStepCount = m_nCount;
	m_prm.m_nUseKm = m_useKM;
	m_prm.m_nSameRange = m_bRangeSame;
	m_prm.m_nUseSelectedTime = m_bUseSelectTime;
	m_prm.Update();

	UpdateData(FALSE);
	CDialog::OnOK();
}

BOOL CStartCalcParamDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	if(m_useKM)
	{
		m_dShift = CEphemWrapper::AUtoKM(m_dShift);
		m_dFromRange = CEphemWrapper::AUtoKM(m_dFromRange);
		m_dToRange = CEphemWrapper::AUtoKM(m_dToRange); 
	}

	if(m_bUseSelectTime)
	{
		m_dFromTime = m_dFromSelTime;
		m_dToTime = m_dToSelTime;
	}
	else
	{
		m_dFromTime = m_dFromDefTime;
		m_dToTime = m_dToDefTime;
	}

	GetDlgItem(IDC_EDIT_SHOW_TO)->SetWindowText(CEphemWrapper::StrDateFromJD(m_dToTime));
	GetDlgItem(IDC_EDIT_SHOW_FROM)->SetWindowText(CEphemWrapper::StrDateFromJD(m_dFromTime));

	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CStartCalcParamDlg::OnEnChangeEditFrom()
{
	UpdateData(TRUE);
	if(m_bRangeSame)
		m_dToRange = -m_dFromRange;
	UpdateData(FALSE);
}

void CStartCalcParamDlg::OnEnChangeEditTo()
{
	UpdateData(TRUE);
	if(m_bRangeSame)
		m_dFromRange = -m_dToRange;
	UpdateData(FALSE);
}

void CStartCalcParamDlg::OnBnClickedButtonFromSel()
{
	m_dFromTime = m_dFromSelTime;
	GetDlgItem(IDC_EDIT_SHOW_FROM)->SetWindowText(CEphemWrapper::StrDateFromJD(m_dFromTime));
	UpdateData(FALSE);
}

void CStartCalcParamDlg::OnBnClickedButtonFromDef()
{
	m_dFromTime = m_dFromDefTime;
	GetDlgItem(IDC_EDIT_SHOW_FROM)->SetWindowText(CEphemWrapper::StrDateFromJD(m_dFromTime));
	UpdateData(FALSE);
}

void CStartCalcParamDlg::OnBnClickedButtonToSel()
{
	m_dToTime = m_dToSelTime;
	GetDlgItem(IDC_EDIT_SHOW_TO)->SetWindowText(CEphemWrapper::StrDateFromJD(m_dToTime));
	UpdateData(FALSE);
}

void CStartCalcParamDlg::OnBnClickedButtonToDef()
{
	m_dToTime = m_dToDefTime;
	GetDlgItem(IDC_EDIT_SHOW_TO)->SetWindowText(CEphemWrapper::StrDateFromJD(m_dToTime));
	UpdateData(FALSE);
}

void CStartCalcParamDlg::OnEnChangeEditTimeFrom()
{
	UpdateData(TRUE);
	GetDlgItem(IDC_EDIT_SHOW_FROM)->SetWindowText(CEphemWrapper::StrDateFromJD(m_dFromTime));
}

void CStartCalcParamDlg::OnEnChangeEditTimeTo()
{
	UpdateData(TRUE);
	GetDlgItem(IDC_EDIT_SHOW_TO)->SetWindowText(CEphemWrapper::StrDateFromJD(m_dToTime));
}

void CStartCalcParamDlg::OnBnClickedCheckUseSelectTime()
{
	UpdateData(TRUE);
	if(m_bUseSelectTime)
	{
		OnBnClickedButtonFromSel();
		OnBnClickedButtonToSel();
	}
	else
	{
		OnBnClickedButtonFromDef();
		OnBnClickedButtonToDef();
	}
}
